layui.use(["layer","jquery","form","upload","layedit","element"],function () {
    var layer = layui.layer;
    var $ = layui.jquery;
    var form = layui.form;
    var upload = layui.upload;
    var layedit = layui.layedit;
    var element = layui.element;
    var creatDate = "";

    //刷新
    $("#refresh").click(function () {
        location.reload();
    })
    //返回
    $('#goBack').click(function () {
        parent.tabParentChange(3);
    })
    $(document).click(function () {
        parent.$('.rightMenu').hide();
    });

    var user = localStorage.getItem('user');
    user = JSON.parse(user);
    var userId = user.data.userId;

    //点击小图标
    microcircleClass = function microcircleClass() {
        layer.open({
            type:2,
            title:"文章分类",
            area:["500px","500px"],
            offset:['150px','500px'],
            content:"article-classification.html",
        });
    }

    //上传图片
    var arr2 = [];
    upload.render({
        elem: "#upLoad",
        url: "/Upload/uploadFiles",
        method: "post",
        field: 'file',
        accept: 'file',//上传文件类型
        auto: false,//关闭自动上传
        bindAction: "#start",
        multiple: true,
        before: function (obj) {
            layer.msg('图片上传中...', {
                icon: 16,
                shade: 0.01,
                time: 0
            })
        },
        choose: function (obj) {//走缓存
            //将每次选择的文件追加到文件队列
            var files = obj.pushFile();
            //预读本地文件，如果是多文件，则会遍历。(不支持ie8/9)
            obj.preview(function (index, file, result) {
                if (file.size > 0 && $('#imgurls').find('img').length === 0) {
                    $('#imgurls').empty();
                }
                // 添加图片 ImgPreview-预览的dom元素的id
                $('#imgurls').append('<div style="float: left;" class="file-iteme" id="container' + index + '"><div class="handle"><i id="upload_img_' + index + '" class="layui-icon" style="color: white ;margin-right: 40%">&#xe640;</i></div>' +
                    '<img id="showImg' + index + '" style="cursor:pointer;"src="' + result + '" alt="' + file.name + '"></div>');
                //删除某图片
                $("#upload_img_" + index).bind('click', function () {
                    delete files[index];
                    $("#container" + index).remove();
                });
            });
        },
        done: function (res) {
            //上传完毕
            arr2.push(res.data);
            element.progress('imgProgress', '100%');
            layer.msg("上传成功");
        },
        /*allDone: function(){
            $("#SubmitProduct").click();
        }*/
    });

    $(document).on("mouseenter mouseleave", ".file-iteme", function (event) {
        if (event.type === "mouseenter") {
            //鼠标悬浮
            // $(this).children(".info").fadeIn("fast");
            $(this).children(".handle").fadeIn("fast");
        } else if (event.type === "mouseleave") {
            //鼠标离开
            // $(this).children(".info").hide();
            $(this).children(".handle").hide();
        }
    });


    //保存草稿
    var status = "";
    saveDraft = function saveDraft() {
        status = $("#status").val();
        //给图片路径赋值
        for (var i = 0; i < arr2.length; i++) {
            if (i == 0) {
                $("#artcleUrl").val(arr2[i]);
            }
        }
        var layForm = form.val("layForm");
        getTime();
        microcircleData = {
            "acticleCategroyId":layForm.acticleCategroyId,
            "article":layForm.area,
            "artcleTitle" : layForm.artcleTitle,
            "userId": userId,
            "createDate": creatDate,
            "details": 2,
            "artcleUrl":layForm.artcleUrl,
            "acticleShare": 0,
            "acticleReviews": 0,
            "acticleLike":0,
            "acticleHits":0,
            "show": 2,
            "artcleKeywords":layForm.artcleKeywords

        }
        if(status == 2){//保存草稿
            if(layForm.acticleCategroyId != null && layForm.acticleCategroyId != ''&& layForm.artcleTitle != null && layForm.artcleTitle != '' && layForm.area != null && layForm.area != '' && layForm.artcleUrl!=null && layForm.artcleUrl!=''){
                $.ajax({
                    url: '/microcircle/addMicrocircle',
                    type: 'post',
                    dataType: 'json',
                    data: microcircleData,
                    success: function (data) {
                        var statu = $("#status").val(1);
                        status = statu;
                        layer.msg("草稿保存成功");
                    }
                })
            }else{
                if (layForm.artcleTitle == null || layForm.artcleTitle == ''){
                    layer.msg("当前文章没有标题");
                    return ;
                }
                if (layForm.acticleCategroyId == null || layForm.acticleCategroyId == ''){
                    layer.msg("请选择微圈分类");
                    return ;
                }
                if (layForm.artcleUrl == null || layForm.artcleUrl == ''){
                    layer.msg("请上传图片");
                    return ;
                }
                if (layForm.area == null || layForm.area == ''){
                    layer.msg("当前文章没有内容");
                    return ;
                }
            }
            console.log(status);
        }else if(status == 1){//修改草稿
            console.log(status);
            $.ajax({//查询状态为2的微圈
                url: '/microcircle/getMicrocircleBydetails',
                type: 'post',
                dataType: 'json',
                data: {"details" : 2},
                success: function (data) {
                    var microcircleId = data.data[0].microcircleId
                    data = {
                        "microcircleId":microcircleId,
                        "artcleTitle" : layForm.artcleTitle,
                        "artcleUrl" : layForm.artcleUrl,
                        "acticleCategroyId" : layForm.acticleCategroyId,
                        "article" : layForm.area,
                    }
                    $.ajax({//微圈修改 根据microcircleId
                        url: '/microcircle/editMicrocircle',
                        type: 'post',
                        dataType: 'json',
                        data: data,
                        success: function (data) {
                            layer.msg("修改成功，可以发布文章啦！！");
                        }
                    })
                }
            });
        }
    }

    //发布文章 发布时间
    release = function release(){//查询草稿状态为2的进行修改
        var layForm = form.val("layForm");
        if(layForm.status == 1){//修改微圈状态为为已提交
            $.ajax({//查询状态为2的微圈
                url: '/microcircle/getMicrocircleBydetails',
                type: 'post',
                dataType: 'json',
                data: {"details" : 2},
                success: function (data) {
                    var microcircleId = data.data[0].microcircleId
                    data = {
                        "microcircleId": microcircleId,
                        "releaseDate": creatDate,
                        "details" : 5
                    }
                    $.ajax({//微圈修改 根据microcircleId
                        url: '/microcircle/editMicrocircleBymicrocircleId',
                        type: 'post',
                        dataType: 'json',
                        data: data,
                        success: function (data) {
                            layer.msg("微圈发布成功,等待审核");
                            //添加审核表
                            addAuditInformation(microcircleId);
                        }
                    })
                }
            });
        }else if (status == 2){
            layer.msg("保存草稿未成功，请填写正确的内容");
        } else if(status == ""){
            console.log(status);
            //给图片路径赋值
            for (var i = 0; i < arr2.length; i++) {
                if (i == 0) {
                    $("#artcleUrl").val(arr2[i]);
                }
            }
            var layForm = form.val("layForm");
            getTime();
            microcircleData = {
                "acticleCategroyId":layForm.acticleCategroyId,
                "article":layForm.area,
                "artcleTitle" : layForm.artcleTitle,
                "userId": userId,
                "createDate": creatDate,
                "details": 2,
                "artcleUrl":layForm.artcleUrl,
                "acticleShare": 0,
                "acticleReviews": 0,
                "acticleLike":0,
                "acticleHits":0,
                "show": 2,
                "artcleKeywords":layForm.artcleKeywords

            }
            if(layForm.acticleCategroyId != null && layForm.acticleCategroyId != ''&& layForm.artcleTitle != null && layForm.artcleTitle != '' && layForm.area != null && layForm.area != '' && layForm.artcleUrl!=null && layForm.artcleUrl!=''){
                $.ajax({
                    url: '/microcircle/addMicrocircle',
                    type: 'post',
                    dataType: 'json',
                    data: microcircleData,
                    success: function (data) {
                        $.ajax({//查询状态为2的微圈
                            url: '/microcircle/getMicrocircleBydetails',
                            type: 'post',
                            dataType: 'json',
                            data: {"details" : 2},
                            success: function (data) {
                                var microcircleId = data.data[0].microcircleId
                                data = {
                                    "microcircleId": microcircleId,
                                    "releaseDate": creatDate,
                                    "details" : 5
                                }
                                $.ajax({//微圈修改 根据microcircleId
                                    url: '/microcircle/editMicrocircleBymicrocircleId',
                                    type: 'post',
                                    dataType: 'json',
                                    data: data,
                                    success: function (data) {
                                        //添加审核表
                                        addAuditInformation(microcircleId);
                                    }
                                })
                            }
                        });
                    }
                })
            }else{
                if (layForm.artcleTitle == null || layForm.artcleTitle == ''){
                    layer.msg("当前文章没有标题");
                    return ;
                }
                if (layForm.acticleCategroyId == null || layForm.acticleCategroyId == ''){
                    layer.msg("请选择微圈分类");
                    return ;
                }
                if (layForm.artcleUrl == null || layForm.artcleUrl == ''){
                    layer.msg("请上传图片");
                    return ;
                }
                if (layForm.area == null || layForm.area == ''){
                    layer.msg("当前文章没有内容");
                    return ;
                }
            }
        }
    }

    //添加审核信息
    function addAuditInformation(microcircleId) {
        data = {
            "courseId":microcircleId,
            "applyDate":creatDate,
            "launchId": userId,
            "result":2,
            "type" : 4
        }
        $.ajax({
            url:"/microcircle/addCourseRecord",
            type:"post",
            dataType:"json",
            data: data,
            success:function (data) {
                layer.msg("添加成功，待审核");
                parent.addTab(23);
            }
        });
    }

    //现在时间
    function getTime(){
        var date=new Date();
        var year=date.getFullYear();
        var month=date.getMonth()+1;
        var day=date.getDate();
        var hour=date.getHours();
        var minute=date.getMinutes();
        var second=date.getSeconds();
        var dateStr = year+"-"+zeroFilling(month)+"-"+zeroFilling(day)+" "+zeroFilling(hour)+":"+zeroFilling(minute)+":"+zeroFilling(second);
        creatDate = dateStr;
    }
    function zeroFilling(data){
        if(data<10){
            data="0"+data;
        }
        return data;
    }
})